package com.sun.mybatisplus.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Auth: syh
 * @Date: 2021/10/22 14:30
 * @Email: 1014170249@qq.com
 * @Desc: 不在能知，而在能行
 * RabbitMq的配置信息
 */
@Configuration
public class RabbitMqConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }


    /**
     * 监听sun.news这个队列，有数据就开始消费，并打印语句
     */
//    @RabbitListener(queues = "my_queue")
//    public void consumMessage() {
//        System.out.println("我消费了信息....");
//    }


    /**
     * 注册一个广播的交换机
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("order_exchange",true,false,null);
    }


    /**
     * sms队列
     * @return
     */
    @Bean
    public Queue smsQueue(){
        return new Queue("sms_fanout.queue",true);
    }

    /**
     * 邮箱队列
     * @return
     */
    @Bean
    public Queue emailQueue(){
        return new Queue("email_fanout.queue",true);
    }

    /**
     * 短信队列
     * @return
     */
    @Bean
    public Queue duanxinQueue(){
        return new Queue("duanxin_fanout.queue",true);
    }

    /**
     * 通过路由key进行绑定
     * @return
     */
    @Bean
    public Binding smsBinding(){
        return BindingBuilder.bind(smsQueue()).to(fanoutExchange());
    }

    /**
     * 通过路由key进行绑定
     * @return
     */
    @Bean
    public Binding emailBinding(){
        return BindingBuilder.bind(emailQueue()).to(fanoutExchange());
    }

    /**
     * 通过路由key进行绑定
     * @return
     */
    @Bean
    public Binding duanxinBinding(){
        return BindingBuilder.bind(duanxinQueue()).to(fanoutExchange());
    }

}
